﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using uTools;
public class Level3MoveUpLetters : MonoBehaviour
{
    public Text targetText;
    public List<TextButton> tbList = new List<TextButton>();
    public List<uTweenPosition> tpList = new List<uTweenPosition>();
    private uTweenPosition curUtp;
    private string tempStr = string.Empty;
    public GameLevelItem gameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitTextButton();
        ShowRandomLetter();
        targetText.text = string.Empty;
    }

    private void InitTextButton()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            tbList[i].Init("o", () =>
            {
                OnTextButtonClick(tempIndex);
            });
        }
    }

    private void OnTextButtonClick(int index)
    {
        tempStr += tbList[index].text.text;
        targetText.text = tempStr;
        if (tempStr.Length == UConst.Str_TargetLove.Length)
        {
            gameLevelItem.TriggerResult(UConst.Str_TargetLove.Equals(tempStr));
        }
    }

    private void ShowRandomLetter()
    {
        int randomIndex = Random.Range(0, 3);
        tbList[randomIndex].text.text = GetRandomLetter();
        curUtp = tpList[randomIndex];
        curUtp.onFinished.RemoveAllListeners();
        curUtp.onFinished.AddListener(() =>
        {
            Invoke("HideRandomLetter", 0.6f);
        });
        curUtp.Play(PlayDirection.Forward);

    }

    private void HideRandomLetter()
    {
        curUtp.onFinished.RemoveAllListeners();
        curUtp.onFinished.AddListener(() =>
        {
            Invoke("ShowRandomLetter", 0.5f);
        });
        curUtp.Play(PlayDirection.Reverse);
    }



    private string GetRandomLetter()
    {
        string letter = "";
        int randomValue = Random.Range(0, 4);
        if (randomValue == 0)
            letter = "l";
        else if (randomValue == 1)
            letter = "o";
        else if (randomValue == 2)
            letter = "v";
        else if (randomValue == 3)
            letter = "e";
        return letter;
    }


}
